package com.hmys.api.controller;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jsms.api.JSMSClient;
import cn.jsms.api.ValidSMSResult;
import com.hmys.api.config.OSSConfig;
import com.hmys.api.config.SMSConfig;
import com.hmys.api.model.WebHelpFindHouse;
import com.hmys.api.model.WebUser;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.service.WebHelpFindHouseService;
import com.hmys.api.utils.ValidationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by chen365 on 2018/04/11.
 */
@RestController
@Api(tags = "帮我找房")
@RequestMapping(value = "/help/find/house")


public class HelpFindHouseController {

    @Autowired
    private WebHelpFindHouseService webHelpFindHouseService;

    @Autowired
    private SMSConfig smsConfig;

    @ApiOperation(value = "帮我找房", notes = "帮我找房", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "msg", value = "短信", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "msgId", value = "短信Id", required = true, paramType = "query"),

            @ApiImplicitParam(dataType = "String", name = "name", value = "名字", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "phone", value = "手机", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "email", value = "邮箱", required = false, paramType = "query"),

            @ApiImplicitParam(dataType = "String", name = "investmentObjective", value = "投资目的", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "capitalSource", value = "资金来源，国内资金，海外资金", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "tendencyRoom", value = "房型倾向", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "tendencySite", value = "地点倾向", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "tendencyPrice", value = "价格倾向", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "tendencySize", value = "面积倾向", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "tendencySchool", value = "学校倾向(幼儿园,小学,初中,高中,大学)", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "留言", required = false, paramType = "query")
    })
    @PostMapping(value = "/save")
    public Object register(String msg, String msgId, String name, String email, String phone,
                           String investmentObjective, String capitalSource, String tendencyRoom,
                           String tendencySite, String tendencyPrice, String tendencySize, String tendencySchool,String message) {

        if (StringUtils.isEmpty(name)) {
            return new Result(ResultCode.EMPTY_NAME);
        }

        if (!StringUtils.isEmpty(email)) {
            if (!ValidationUtil.isEmail(email))
                return new Result(ResultCode.ERROR_EMAIL);
        }
        if (!ValidationUtil.isPhone(phone)) {
            return new Result(ResultCode.ERROR_PHONE);
        }

        if (StringUtils.isEmpty(msg)) {
            return new Result(ResultCode.EMPTY_SMS_VALIDATE_CODE);
        }

        if (StringUtils.isEmpty(msgId)) {
            return new Result(ResultCode.EMPTY_MSG_ID);
        }

        WebHelpFindHouse record = new WebHelpFindHouse();

        record.setPhone(phone);
        record.setName(name);
        record.setEmail(email);

        if (!StringUtils.isEmpty(investmentObjective)) {
            record.setInvestmentObjective(investmentObjective);
        }
        if (!StringUtils.isEmpty(capitalSource)) {
            record.setCapitalSource(capitalSource);
        }
        if (!StringUtils.isEmpty(tendencyRoom)) {
            record.setTendencyRoom(tendencyRoom);
        }
        if (!StringUtils.isEmpty(tendencySite)) {
            record.setTendencySite(tendencySite);
        }
        if (!StringUtils.isEmpty(tendencyPrice)) {
            record.setTendencyPrice(tendencyPrice);
        }
        if (!StringUtils.isEmpty(tendencySize)) {
            record.setTendencySize(tendencySize);
        }
        if (!StringUtils.isEmpty(tendencySchool)) {
            record.setTendencySchool(tendencySchool);
        }
        if (!StringUtils.isEmpty(message)) {
            record.setMessage(message);
        }

        try {
            JSMSClient jsmsClient = new JSMSClient(smsConfig.getSecret(), smsConfig.getAppkey());
            ValidSMSResult result = jsmsClient.sendValidSMSCode(msgId, msg);
            if (result.getIsValid()) {
                if (webHelpFindHouseService.insert(record) == 1) {
                    return new Result(ResultCode.SUCCESS);
                }
            } else {
                return new Result(ResultCode.ERROR_VALIDATE_CODE);
            }
        } catch (APIConnectionException e1) {
            e1.printStackTrace();
        } catch (APIRequestException e1) {
            e1.printStackTrace();
        }
        return new Result(ResultCode.FAIL);
    }
}
